from image_capturer import GameCapturer
from state_analyzer import StateAnalyzer
from decision_client import DecisionClient
from action_executor import ActionExecutor


class PlantSelectionAgent:
    def __init__(self):
        self.capturer = GameCapturer()
        self.analyzer = StateAnalyzer()
        self.client = DecisionClient()
        self.executor = ActionExecutor()

    def run(self):
        while True:
            # 1. 获取游戏画面
            screen = self.capturer.capture_screen()

            # 2. 解析当前状态
            game_state = self.analyzer.analyze_plant_selection(screen)

            # 3. 请求决策
            try:
                decision = self.client.make_decision_request(game_state)
            except DecisionError as e:
                self.handle_error(e)
                continue

            # 4. 执行动作
            self.executor.execute_actions(decision['actions'])

            # 5. 验证结果
            if self.check_completion():
                break

    def check_completion(self):
        """检查是否完成植物选择"""
        # 通过检测"Start"按钮状态确认
        screen = self.capturer.capture_screen()
        return self.analyzer.detect_start_button(screen)